# 雷达图数据（归一化到0-1，值越小越好）
categories = ['Communication', 'Energy', 'Latency']
fedavg_norm = [1.0, 1.0, 0.96]  # FedAvg作为基准1.0
fedprox_norm = [0.90, 0.88, 1.0] 
fedat_norm = [0.69, 0.69, 0.83]
dhaf_norm = [0.13, 0.37, 0.65]  # DHA-FL实际值/FedAvg值

# 闭合雷达图
categories += [categories[0]]
fedavg_norm += [fedavg_norm[0]]
fedprox_norm += [fedprox_norm[0]]
fedat_norm += [fedat_norm[0]]
dhaf_norm += [dhaf_norm[0]]

# 角度计算
label_loc = np.linspace(start=0, stop=2*np.pi, num=len(categories))

# 绘制雷达图
plt.figure(figsize=(8, 8))
ax = plt.subplot(polar=True)
ax.plot(label_loc, fedavg_norm, 'b-', linewidth=2, label='FedAvg')
ax.fill(label_loc, fedavg_norm, 'b', alpha=0.1)
ax.plot(label_loc, fedprox_norm, 'g--', linewidth=2, label='FedProx')
ax.fill(label_loc, fedprox_norm, 'g', alpha=0.1)
ax.plot(label_loc, fedat_norm, 'r-.', linewidth=2, label='FedAT')
ax.fill(label_loc, fedat_norm, 'r', alpha=0.1)
ax.plot(label_loc, dhaf_norm, 'm-', linewidth=3, label='DHA-FL')
ax.fill(label_loc, dhaf_norm, 'm', alpha=0.2)

# 设置标签
ax.set_xticks(label_loc[:-1])
ax.set_xticklabels(categories[:-1],fontsize=12)
ax.set_rlabel_position(30)
plt.yticks([0.2, 0.4, 0.6, 0.8, 1.0], ['0.2', '0.4', '0.6', '0.8', '1.0'], color="gray", size=10)
plt.ylim(0, 1.1)

# 标注面积比
fedavg_area = 0.5 * sum(fedavg_norm[:-1]) * np.sin(2*np.pi/3)
dhaf_area = 0.5 * sum(dhaf_norm[:-1]) * np.sin(2*np.pi/3)
plt.text(0.5, 0.5, f'Area Ratio: {dhaf_area/fedavg_area:.1%}', 
         transform=ax.transAxes, fontsize=12)

# plt.title('Comprehensive Efficiency Radar Chart', fontsize=14, pad=20)
plt.legend(loc='lower left', bbox_to_anchor=(0.8, 0.1),prop={'size': 14, 'family': 'SimHei'})
plt.savefig('efficiency_radar_en.png', bbox_inches='tight')
